﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using XuanFe.Database.Model;

namespace XuanFe.Database.DAL
{
    public class HintDAL
    {
        private const string SQL_SELECT_WS_HINT = "select HIT_HINT_ID as HINT_ID,HIT_REGISTER_ID as REGISTER_ID,HIT_HINT_TYPE as HINT_TYPE,HIT_HINT_DESC as HINT_DESC,HIT_FOLLOW_STAFF_ID as FOLLOW_STAFF_ID,HIT_HINT_TIME as HINT_TIME,HIT_HANDLED_FLG as HANDLED_FLG,HIT_RCRE_STAFF_ID as RCRE_STAFF_ID,HIT_RCRE_TIME as RCRE_TIME,HIT_LCHG_STAFF_ID as LCHG_STAFF_ID,HIT_LCHG_TIME as LCHG_TIME from WS_HINT where 1=1";

        private const string SQL_WHERE_HINT_TYPE = " and HIT_HINT_TYPE=@HintType ";
        private const string SQL_WHERE_FOLLOW_STAFF_ID = " and HIT_FOLLOW_STAFF_ID=@FollowStaffId ";
        private const string SQL_WHERE_HANDLED_FLG = " and HIT_HANDLED_FLG=@HandledFlag ";

        private const string SQL_ORDER_BY_HINT_TIME = " order by HINT_TIME ";

        private const string PARM_HINT_TYPE = "@HintType";
        private const string PARM_FOLLOW_STAFF_ID = "@FollowStaffId";
        private const string PARM_HANDLED_FLG = "@HandledFlag";

        private static HintDAL instance = new HintDAL();
        /// <summary>
        /// 初始化实例
        /// </summary>
        /// <returns></returns>
        public static HintDAL Instance
        {
            get
            {
                return instance;
            }
        }
        public DataSet GetDataSet(QueryHintModel model)
        {
            StringBuilder sbSQL = new StringBuilder(SQL_SELECT_WS_HINT);
            List<MySqlParameter> parms = new List<MySqlParameter>();

            if (model != null)
            {
                if (model.QueryFollowStaffId)
                {
                    sbSQL.Append(SQL_WHERE_FOLLOW_STAFF_ID);
                    parms.Add(new MySqlParameter(PARM_FOLLOW_STAFF_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = model.FollowStaffId });
                }
                if (model.QueryHintType)
                {
                    sbSQL.Append(SQL_WHERE_HINT_TYPE);
                    parms.Add(new MySqlParameter(PARM_HINT_TYPE, MySqlDbType.Int32) { Direction = ParameterDirection.Input, Value = model.HintType });
                }
                if (model.QueryHandled)
                {
                    sbSQL.Append(SQL_WHERE_HANDLED_FLG);
                    parms.Add(new MySqlParameter(PARM_HANDLED_FLG, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = model.Handled ? "Y" : "N" });
                }
            }
            sbSQL.Append(SQL_ORDER_BY_HINT_TIME);

            return MySQLHelper.Instance.ExecuteDataSet(MySQLHelper.Instance.ConnectionString, CommandType.Text, sbSQL.ToString(), null);
        }
    }
}
